#!/usr/bin/env python
#
# Copyright 2008 Allen Hutchison (allen@hutchison.org)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# $URL: http://severedelays.googlecode.com/svn/trunk/appengine/lib/scrapers/uk/lon/station_parser.py $
__version__ = "$Rev: 51 $"
__author__ = "Allen Hutchison (allen@hutchison.org)"

"""Parses the TFL live status page

This class parses the TFL live status page and returns an object that allows you
to get the status of the lines. 
"""

from google.appengine.api import urlfetch

from third_party.BeautifulSoup import BeautifulSoup


class TflParser():
  def __init__(self):
    self.url = "http://www.tfl.gov.uk/tfl/livetravelnews/realtime/tube/default.html"
    self.status = {}
    self.getContent();

  def getContent(self):
    self.result = urlfetch.fetch(self.url)
    if ((self.result.status_code == 200) and
      (self.result.content_was_truncated == 0)):
      self.soup = BeautifulSoup(self.result.content)
      block = self.soup.find('dl', id='lines')
      lines = block.findAll('dt')
      status = block.findAll('dd')
      ziplist = [lines, status]
      for x in zip(*ziplist):
        line = x[0]['class']
        self.status[line] = {}
        
        if "Good service" == x[1].string:
          self.status[line]['status'] = 'goodservice'
          self.status[line]['description'] = ''
        elif "Minor delays" == x[1].find('h3').string:
          self.status[line]['status'] = 'minordelays'
          self.status[line]['description'] = x[1].find('p').string
        elif "Severe delays" == x[1].find('h3').string:
          self.status[line]['status'] = 'severedelays'
          self.status[line]['description'] = x[1].find('p').string
        elif "Part suspended" == x[1].find('h3').string:
          self.status[line]['status'] = 'partsuspended'
          self.status[line]['description'] = x[1].find('p').string
        elif "Suspended" == x[1].find('h3').string:
          self.status[line]['status'] = "suspended"
          self.status[line]['description'] = x[1].find('p').string
        elif "Part closure" == x[1].find('h3').string:
          self.status[line]['status'] = "partclosure"
          self.status[line]['description'] = x[1].find('p').string
        elif "Planned closure" == x[1].find('h3').string:
          self.status[line]['status'] = "plannedclosure"
          self.status[line]['description'] = x[1].find('p').string
  

  def getLines(self):
    return self.status.keys()

  def getStatus(self, line):
    return str(self.status[line]['status'])

  def getDescription(self, line):
    return str(self.status[line]['description'])

def main():
  tp = TflParser()
  for x in tp.getLines():
    print x, tp.getStatus(x), tp.getDescription(x)

if __name__ == '__main__':
  main()